import {Component, Vue} from 'vue-property-decorator';
import $http from "@/core/utils/http";

/**
 * 新版本
 * 顶层处理类
 */
@Component({
    name: 'BaseVue',
})
export default class BaseVue extends Vue {
    /**
     * 多视图切换-默认处理了增删改，其余的在组件内自行处理吧
     * @viewData 通信数据
     * @viewOption   通信选项
     * @showList 展示列表页
     * @showEdit 展示编辑页
     * @showInfo 展示详情页
     */
    viewData: any = {};
    viewOption: any = {};
    showList = true;
    showEdit = false;
    showInfo = false;


    showMoreSearch: boolean = false;


    /**
     * 表格
     * @tableData 表格数据
     * @selectedRows 选中的数据
     * @selectedRowKeys 选中的ID
     *
     * @selectionChange() 复选框事件函数
     */
    tableData: Array<any> = [];
    selectedRows: Array<any> = [];
    selectedRowKeys: Array<any> = [];

    selectionChange(val: any) {
        this.selectedRows = val;
        val.filter((it: any) => {
            this.selectedRowKeys.push(it['id'])
        });
    }

    /**
     * 查询
     * @queryParam 查询条件
     * @clearParam() 清空查询条件函数
     */
    queryParam: any = {};

    clearParam() {
        this.queryParam = {};
    }

    /**
     * 分页
     * @pageParam 分页参数
     */
    pageParam: any = {
        size: 10,
        current: 1
    };


    /**
     * 查询分页方法
     *
     * @url    请求地址
     * @page   分页参数
     * @config http配置
     *
     * @return Promise
     */
    async pageData(url: any, page: any = null, config: any = null) {
        this.tableData = [];
        if (page) {
            this.pageParam.size = page.pageSize;
            this.pageParam.current = page.currentPage;
        }
        Object.assign(this.queryParam, this.pageParam);
        return $http.post(url, this.queryParam, config)
            .then((res: any) => {
                if (res.success) {
                    this.tableData = res.data['records'];
                    //全部删除后，跳转到上一页
                    if (this.tableData.length == 0 && this.pageParam.current > 0) {
                        this.pageParam.current = ((this.pageParam.current * 1) - 1);
                        this.pageData(url, null, config);
                        return res;
                    }
                    //更新分页
                    this.pageParam.size = res.data['size'];
                    this.pageParam.current = res.data['current'];
                    this.pageParam.currentSize = res.data['records'].length;
                    this.pageParam.pages = res.data['pages'];
                    this.pageParam.total = res.data['total'];
                }
                return res;
            });
    }
}
